import { outputFileSync } from 'fs-extra';
import { join, resolve } from 'path';
import { AutoBase } from './auto-base'
export enum fileType {
    'ts' = ".tsx",
    'none' = ".jsx"
}
export type typefileType = {
    type: 'ts' | 'none'
}
export class ReactAuto extends AutoBase {
    constructor() {
        super()
    }
    file_type = '.jsx'
    createImport() {
        let arr = this.getAllJson().map(ele => {
            if (ele.query) {
                let query = Object.keys(ele.query);
                // ele.component = 
                if (query.length > 0) {
                    query.forEach(ele2 => {
                        ele.ClonePath = ele.path + '/:' + ele2
                    })
                }
            }
            return ele
        })
        let importStr = ''
        arr.forEach(ele => {
            let str = `{name:'${ele.name}' ,path: '${ele.ClonePath || ele.path}', component: lazy(() => import('@${ele.path}${this.file_type}'))},`
            importStr += str
        })

        let str = `const routerArr = [${importStr}];export default routerArr;
         `
        return str
    }
    creatFixed() {
        return `import  { lazy  } from 'react';`
    }

    writeFile() {
        let json = this.getConfigJson();
        let str = '';
        let allJson = this.createAllJson()
        this.file_type = fileType[json.type]
        str += this.creatFixed();
        str += this.createImport();
        outputFileSync(join(this.ROOT_DIR, 'router', `index${this.file_type
            }`), str)

        outputFileSync(join(this.ROOT_DIR, 'router', `allPage.json`), allJson)
    }
}
// delete require.cache[require.resolve('./myJson.js')];
// let myJson = require('./myJson.js');
